package com.cyandpos.pad.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cyandpos.pad.R;
import com.cyandpos.pad.cateringformat.morefunction.adapter.CleartableGridAdapter;
import com.cyandpos.pad.cateringformat.morefunction.bean.Table;
import com.cyandpos.pad.cateringformat.morefunction.bean.TableData;
import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;

import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.netdata.Res_MoneyBuyPro;
import com.cyandpos.pad.pay.adapter.PayGridAdapter;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.ToastUtils;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

public class ClearTableActivity extends AppCompatActivity {
    private  TextView  tv_dialogtitle_name;
    private  GridView  gridView;
    private CleartableGridAdapter cleartableAdapter;
    private List<Table> tablelist;
    private AlertDialog mDialog;//所有dialog
    private ProgressDialog mWaitDialog;

    /*
    *   1. 拉取所有脏桌的列表
    *   2. 点击桌子 调用修改状态桌子状态接口
    *   3. 更新状态成功后 重新拉取脏桌
    *   4. 重新拉取的脏桌列表 长度为0 就不弹框了
    * */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clear_table);
        tv_dialogtitle_name= findViewById(R.id.tv_dialog_title_name);
        gridView= findViewById(R.id.gv_act_cleartable);
       Button  clear_exit_btn= findViewById(R.id.clear_exit);
        tablelist = new ArrayList<>();
        mWaitDialog = new ProgressDialog(ClearTableActivity.this);
        updata_table();

        cleartableAdapter = new CleartableGridAdapter(ClearTableActivity.this,tablelist);
        gridView.setAdapter(cleartableAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cleartableAdapter.setSelectPos(position, true);

                //调接口 改变桌子状态
                showDjdDialog(position);
            }
        });

        clear_exit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  finish();
            }
        });

        tv_dialogtitle_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  finish();
            }
        });
    }

    private void updata_table() {

      //  showWaitDialog("查询脏桌列表.....");
        PosHttpManager.querytablesHttpEexcute(2,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.i("TAG----脏桌子列表： ",response.toString());
        //        dismissWaitDialog();
                BaseResponse<TableData> baseResponse = BaseResponse.fromJson(response.toString(), TableData.class);

                if(baseResponse.getCode().equals("0")){
                    tablelist.clear();
                    if (baseResponse.getData().getData()!=null){
                        tablelist.addAll(baseResponse.getData().getData());
                        cleartableAdapter.notifyDataSetChanged();
                    }else {
                        ToastUtils.showMsg("没有脏桌数据");
                    }

                }else{
                    ToastUtils.showMsg("脏桌列表返回数据错误"+baseResponse.getMsg().toString());
                }
            }
            @Override
            public void onFailure(String msg) {
                Log.i("TAG----桌子列表msg： ",msg.toString());

            }
        });

    }

    /**
     * 确认 清桌或取消
     * @param position
     */
    private void showDjdDialog(final int position) {
        //dialog_table_clear_layout.xml
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_table_clear_layout, null);

        Button tableclearOK = (Button) view.findViewById(R.id.btn_tableclear_ok);
        Button tableclearNo = (Button) view.findViewById(R.id.btn_tableclear_back);
        tableclearNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        tableclearOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 mDialog.dismiss();

                showWaitDialog("正在清桌.....");
               //第三个参数 0是空置
                PosHttpManager.updataTableStatusHttpEexcute(tablelist.get(position).getShopid(),tablelist.get(position).getId(),0,new OkHttpUtils.ResultCallback() {
                    @Override
                    public void onSuccess(JsonObject response) {
                        Log.i("TAG----修改清桌状态后返回",response.toString());
                        dismissWaitDialog();
                        updata_table();
                    }
                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(ClearTableActivity.this,"修改清桌状态接口异常",Toast.LENGTH_SHORT);

                        ToastUtils.showMsg("修改清桌状态接口异常:"+msg);
                    }
                });
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(ClearTableActivity.this);
        builder.setView(view).setCancelable(false).create();
        mDialog = builder.show();

    }


    @Override
    protected void onResume() {
        super.onResume();
        tv_dialogtitle_name.setText("请选择要清的桌子");
    }

    public void showWaitDialog(String msg) {
         mWaitDialog.setCancelable(false);
        if (mWaitDialog != null && !mWaitDialog.isShowing()) {
            mWaitDialog.setMessage(msg);
            mWaitDialog.show();
        }
    }

    public void dismissWaitDialog() {
        if (mWaitDialog != null && mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
        }
    }

}
